#include <gflags/gflags.h>
#include <gtest/gtest.h>

#include "logger.hpp"
#include "channel.hpp"
#include "etcd.hpp"
#include "utils.hpp"

#include "base.pb.h"
#include "user.pb.h"
#include "transmite.pb.h"

DEFINE_bool(run_mode, true, "是否为调试模式");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(transmite_service, "/service/transmite_service", "服务信道目录");

class TransmiteClientTest: public ::testing::Test {
protected:
    void SetUp() override {
        auto sm = std::make_shared<im::ServiceManager>();
        sm->declared(FLAGS_transmite_service);
        auto put_cb = std::bind(&im::ServiceManager::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
        auto del_cb = std::bind(&im::ServiceManager::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
        im::Discover::ptr discover = std::make_shared<im::Discover>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
        // 初始化服务发现
        _channel = sm->choose(FLAGS_transmite_service);
        if (!_channel) {
            im::LOG_CRITICAL("未发现关注的服务，测试退出");
            abort() ;
        }
    }

    void TearDown() override {
    }

    im::ServiceChannel::ChannelPtr _channel;
};

TEST_F(TransmiteClientTest, GetTransmitTarget_StringMsg) {
    im::MsgTransmitService_Stub stub(_channel.get());
    im::NewMessageReq req;
    std::string uuid = im::uuid();
    req.set_request_id(uuid);
    req.set_user_id("b3a6-96f15f76-0000");  // 这个是根据数据库里的内容进行针对测试的数据
    req.set_chat_session_id("会话ID1");     //  +1
    req.mutable_message()->set_message_type(im::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content("这是一条文本测试消息");
    im::GetTransmitTargetRsp rsp;
    brpc::Controller cntl;
    // 发起请求
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);

    ASSERT_FALSE(cntl.Failed());
    EXPECT_EQ(rsp.request_id(), uuid);
    EXPECT_TRUE(rsp.success());
    
    // 验证消息内容
    EXPECT_EQ(rsp.message().sender().nickname(), "zhangsan"); //  +1
    EXPECT_EQ(rsp.message().message().string_message().content(), "这是一条文本测试消息"); //  +1

    // 验证转发目标列表
    im::LOG_INFO("会话ID : {}", req.chat_session_id());
    for(auto& uid : rsp.target_id_list()){
        im::LOG_INFO("用户id : {}", uid);
    }
}

TEST_F(TransmiteClientTest, GetTransmitTarget_StringMsg2) {
    im::MsgTransmitService_Stub stub(_channel.get());
    im::NewMessageReq req;
    std::string uuid = im::uuid();
    req.set_request_id(uuid);
    req.set_user_id("b3a6-96f15f76-0000");  // 这个是根据数据库里的内容进行针对测试的数据
    req.set_chat_session_id("会话ID1");     //  +1
    req.mutable_message()->set_message_type(im::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content("关联性消息测试");
    im::GetTransmitTargetRsp rsp;
    brpc::Controller cntl;
    // 发起请求
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);

    ASSERT_FALSE(cntl.Failed());
    EXPECT_EQ(rsp.request_id(), uuid);
    EXPECT_TRUE(rsp.success());
    
    // 验证消息内容
    EXPECT_EQ(rsp.message().sender().nickname(), "zhangsan"); //  +1
    EXPECT_EQ(rsp.message().message().string_message().content(), "关联性消息测试"); //  +1

    // 验证转发目标列表
    im::LOG_INFO("会话ID : {}", req.chat_session_id());
    for(auto& uid : rsp.target_id_list()){
        im::LOG_INFO("用户id : {}", uid);
    }
}

TEST_F(TransmiteClientTest, GetTransmitTarget_ImageMsg) {
    im::MsgTransmitService_Stub stub(_channel.get());
    im::NewMessageReq req;
    std::string uuid = im::uuid();
    req.set_request_id(uuid);
    req.set_user_id("b3a6-96f15f76-0000");  // 这个是根据数据库里的内容进行针对测试的数据
    req.set_chat_session_id("会话ID1");     //  +1
    req.mutable_message()->set_message_type(im::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content("测试图片数据");
    im::GetTransmitTargetRsp rsp;
    brpc::Controller cntl;
    // 发起请求
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);

    ASSERT_FALSE(cntl.Failed());
    EXPECT_EQ(rsp.request_id(), uuid);
    EXPECT_TRUE(rsp.success());
    
    // 验证消息内容
    EXPECT_EQ(rsp.message().sender().nickname(), "zhangsan"); //  +1
    EXPECT_EQ(rsp.message().message().image_message().image_content(), "测试图片数据"); //  +1

    // 验证转发目标列表
    im::LOG_INFO("会话ID : {}", req.chat_session_id());
    for(auto& uid : rsp.target_id_list()){
        im::LOG_INFO("用户id : {}", uid);
    }
}

TEST_F(TransmiteClientTest, GetTransmitTarget_FileMsg) {
    im::MsgTransmitService_Stub stub(_channel.get());
    im::NewMessageReq req;
    std::string uuid = im::uuid();
    req.set_request_id(uuid);
    req.set_user_id("b3a6-96f15f76-0000");  // 这个是根据数据库里的内容进行针对测试的数据
    req.set_chat_session_id("会话ID1");     //  +1
    std::string file_name = "测试文件名";
    std::string file_content = "测试文件内容";
    req.mutable_message()->set_message_type(im::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_name(file_name);
    req.mutable_message()->mutable_file_message()->set_file_size(file_content.size());
    req.mutable_message()->mutable_file_message()->set_file_contents(file_content);
    im::GetTransmitTargetRsp rsp;
    brpc::Controller cntl;
    // 发起请求
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);

    ASSERT_FALSE(cntl.Failed());
    EXPECT_EQ(rsp.request_id(), uuid);
    EXPECT_TRUE(rsp.success());
    
    // 验证消息内容
    EXPECT_EQ(rsp.message().sender().nickname(), "zhangsan"); //  +1
    EXPECT_EQ(rsp.message().message().file_message().file_contents(), file_content); //  +1

    // 验证转发目标列表
    im::LOG_INFO("会话ID : {}", req.chat_session_id());
    for(auto& uid : rsp.target_id_list()){
        im::LOG_INFO("用户id : {}", uid);
    }
}

TEST_F(TransmiteClientTest, GetTransmitTarget_SpeechMsg) {
    im::MsgTransmitService_Stub stub(_channel.get());
    im::NewMessageReq req;
    std::string uuid = im::uuid();
    req.set_request_id(uuid);
    req.set_user_id("b3a6-96f15f76-0000");  // 这个是根据数据库里的内容进行针对测试的数据
    req.set_chat_session_id("会话ID1");     //  +1
    std::string speech_content = "测试语音内容";
    req.mutable_message()->set_message_type(im::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_file_contents(speech_content);
    im::GetTransmitTargetRsp rsp;
    brpc::Controller cntl;
    // 发起请求
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);

    ASSERT_FALSE(cntl.Failed());
    EXPECT_EQ(rsp.request_id(), uuid);
    EXPECT_TRUE(rsp.success());
    
    // 验证消息内容
    EXPECT_EQ(rsp.message().sender().nickname(), "zhangsan"); //  +1
    EXPECT_EQ(rsp.message().message().speech_message().file_contents(), speech_content); //  +1

    // 验证转发目标列表
    im::LOG_INFO("会话ID : {}", req.chat_session_id());
    for(auto& uid : rsp.target_id_list()){
        im::LOG_INFO("用户id : {}", uid);
    }
}

int main(int argc, char *argv[]) {
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    im::initLogger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    return RUN_ALL_TESTS();
}